package com.exam.scjp5.objectives.section_1;

public class ExampleOverloading
{
    public static void main(String args[])
    {
        new A().test();
        
        System.out.println("--------------------");
        
        A a = new B();
        // Here, compiler will check whether 'a' has methodB(int) or not.
        // Then, it founds 'a' has 'public void methodB(Integer i)'.
        // So, it won't be a problem to use 'a.methodB(1)' because
        // the value '1' will be boxed and pass to methodB(Integer i).
        // But 'a' is referring the instance of B !!!!
        // So, class 'B' public void methodB(Integer i) will be invoked by polymorphism.
        // If class A does not methodB(Integer i), then....
        // COMPILER WILL COMPLAIN !!!!!!!!!!!
        a.methodB(1); // It's calling class A's public methodB(Integer) by boxing.
        
        // Here, in this case, the reference 'a' is pointing to instance of 'B'
        // so it will call class B's methodB(Integer i) polymorphically.
        a.methodB(new Integer(1));
    }
}

class A
{
    public int integer;
    
    public void methodA(String s)
    {
    }
    
    private void methodB(int i)
    {
        integer = 10;
        System.out.println("private A.methodB() int");
    }
    
    public void methodB(Integer i)
    {        
        System.out.println(integer);
        System.out.println("public A.methodB() Integer");
    }
    
    public void test()
    {
        A a = new B();
        a.methodB(1);
        a.methodB(new Integer(1));
    }
}

class B extends A
{
    // This is not an overriding method. Just overloading,
    // because 'A' has only private methodB(int).
    public void methodB(int i) 
    {
        System.out.println("public B.methodB() int");
    }
    
    public void methodB(Integer i)
    {
        System.out.println(integer);
        System.out.println("public B.methodB() Integer");
    }    
    
    public void anotherMethod()
    {
        try
        {
            assert false;
        }
        catch(AssertionError e)
        {
            
        }
    }
}


